package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.RegistLevel;
import his.entity.User;
import his.persistence.RegistLevelMapper;
import his.service.RegistLevelService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class RegistLevelServiceImpl implements RegistLevelService {

    @Autowired
    RegistLevelMapper registLevelMapper;


    @Override
    public CommonResponse<List<RegistLevel>> getall_registlevels_bycodename(String keywords) {

        QueryWrapper<RegistLevel> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("SequenceNo",keywords);
        List<RegistLevel> registLevelList1=registLevelMapper.selectList(queryWrapper1);
        if(registLevelList1.size()>0){
            return CommonResponse.createForSuccess(registLevelList1);
        }else{
            QueryWrapper<RegistLevel> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("RegistName",keywords);
            List<RegistLevel> registLevelList2=registLevelMapper.selectList(queryWrapper2);
            if(registLevelList2.size()>0){
                return CommonResponse.createForSuccess(registLevelList2);
            }else{
                return CommonResponse.createForError("搜索失败");
            }
        }

    }

    @Override
    public CommonResponse<List<RegistLevel>> delete_registlevels(int sequence_no) {

        QueryWrapper<RegistLevel> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("SequenceNo",sequence_no);
        List<RegistLevel> registLevelList=registLevelMapper.selectList(queryWrapper1);

        if(registLevelList.size()>0){
            QueryWrapper<RegistLevel> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("SequenceNo",sequence_no);
            int result=registLevelMapper.delete(queryWrapper2);
            return CommonResponse.createForSuccess(registLevelList);
        }else{
            return CommonResponse.createForError("删除失败");
        }

    }

    @Override
    public CommonResponse<List<RegistLevel>> getall_registlevels() {

        QueryWrapper<RegistLevel> queryWrapper=new QueryWrapper<>();
        List<RegistLevel> registLevelList=registLevelMapper.selectList(queryWrapper);
        if(registLevelList.size()>0){
            return CommonResponse.createForSuccess(registLevelList);
        }else{
            return CommonResponse.createForError("查询失败");
        }
    }

    @Override
    public CommonResponse<List<RegistLevel>> update_registlevels(int sequence_no, String registCode, String registName, int sequenceNo, BigDecimal registFee, int registQuota) {

        QueryWrapper<RegistLevel> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("SequenceNo",sequence_no);
        List<RegistLevel> registLevelList=registLevelMapper.selectList(queryWrapper1);
        if(registLevelList.size()>0){
            QueryWrapper<RegistLevel> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("SequenceNo",sequence_no);
            RegistLevel registLevel=new RegistLevel();
            registLevel.setRegistCode(registCode);
            registLevel.setRegistName(registName);
            registLevel.setSequenceNo(sequenceNo);
            registLevel.setRegistFee(registFee);
            registLevel.setRegistQuota(registQuota);
            int result=registLevelMapper.update(registLevel,queryWrapper2);
            QueryWrapper<RegistLevel> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("SequenceNo",sequence_no);
            List<RegistLevel> registLevelList3=registLevelMapper.selectList(queryWrapper3);
            return CommonResponse.createForSuccess(registLevelList3);

        }else{
            return CommonResponse.createForError("修改失败");
        }

    }

    @Override
    public CommonResponse<List<RegistLevel>> add_registlevels(String registCode, String registName, int sequenceNo, BigDecimal registFee, int registQuota) {

        QueryWrapper<RegistLevel> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("SequenceNo",sequenceNo);
        List<RegistLevel> registLevelList1=registLevelMapper.selectList(queryWrapper1);
        if(registLevelList1.size()>0){
            return CommonResponse.createForError("新增失败，已存在");
        }else{
            RegistLevel registLevel=new RegistLevel();
            registLevel.setRegistCode(registCode);
            registLevel.setRegistName(registName);
            registLevel.setSequenceNo(sequenceNo);
            registLevel.setRegistFee(registFee);
            registLevel.setRegistQuota(registQuota);
            registLevel.setDelMark(1);
            registLevelMapper.insert(registLevel);
            QueryWrapper<RegistLevel> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("SequenceNo",sequenceNo);
            List<RegistLevel> registLevelList2=registLevelMapper.selectList(queryWrapper2);
            return CommonResponse.createForSuccess(registLevelList2);
        }

    }


}
